package shuanfa;

import java.io.Serializable;
import java.util.List;

/**
 * @author duson
 */
public class MyPoly {

    private List<Corner> corner;

    public List<Corner> getCorner() {
        return corner;
    }

    public void setCorner(List<Corner> corner) {
        this.corner = corner;
    }

    public static void main(String[] args) {
        Point point = new Point(113, 27);
        MyPoly myPoly = new MyPoly();
        boolean b = myPoly.searchPoint(point);
        System.out.println(b);
    }
    public boolean searchPoint(Point point) {

        if (corner == null || corner.size() == 0) {
            return false;
        }

        double x = point.origX();
        double y = point.origY();
        int points = corner.size();
        int hits = 0;
        double lastX = corner.get(points - 1).getPolyX();
        double lastY = corner.get(points - 1).getPolyY();
        double curX, curY;

        for (int i = 0; i < points; lastX = curX, lastY = curY, i++) {
            curX = corner.get(i).getPolyX();
            curY = corner.get(i).getPolyY();
            if (x == curX && y == curY) {
                return true;
            }
            if (curY == lastY) {
                continue;
            }
            double leftX;
            if (curX < lastX) {
                if (x >= lastX) {
                    continue;
                }
                leftX = curX;
            } else {
                if (x >= curX) {
                    continue;
                }
                leftX = lastX;
            }
            double test1, test2;
            if (curY < lastY) {
                if (y < curY || y >= lastY) {
                    continue;
                }
                if (x < leftX) {
                    hits++;
                    continue;
                }
                test1 = x - curX;
                test2 = y - curY;
            } else {
                if (y < lastY || y >= curY) {
                    continue;
                }
                if (x < leftX) {
                    hits++;
                    continue;
                }
                test1 = x - lastX;
                test2 = y - lastY;
            }
            if (test1 < (test2 / (lastY - curY) * (lastX - curX))) {
                hits++;
            }
        }
        return (hits & 1) != 0;
    }

    public static final class Point {

        private final double origX;

        private final double origY;

        private Point(double x, double y) {
            this.origX = x;
            this.origY = y;
        }

        public double origX() {
            return this.origX;
        }

        public double origY() {
            return this.origY;
        }
    }

    public static class Corner implements Serializable {

        private int polyX;

        private int polyY;

        public Corner(double polyX, double polyY) {
            this.polyX = (int) (polyX * 1000000d);
            this.polyY = (int) (polyY * 1000000d);
        }

        public double getPolyX() {
            return polyX / 1000000d;
        }

        public void setPolyX(int polyX) {
            this.polyX = polyX;
        }

        public double getPolyY() {
            return polyY / 1000000d;
        }

        public void setPolyY(int polyY) {
            this.polyY = polyY;
        }
    }
}

